﻿

Imports System.Data
Imports System.Data.SqlClient


''' <summary>
''' 來電內容--備注
''' </summary>
''' <remarks>
''' Author:huyang
''' Create Date:2008-06-11
''' </remarks>
Public Class Frm_CSRemarkNew

    Implements IDServer.IDServer.IDServer

#Region " Valiabled Define "

#Region "System Variabled Define"


    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Private WithEvents MainForm As Frm_MainForm

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

    Public FindBool As Boolean = True

#End Region

#Region "user variabled define"

    ''' <summary>
    ''' 窗體是否加載
    ''' </summary>
    ''' <remarks></remarks>
    Private fLoadBool As Boolean = False

    Public Fields As String

    Private dbType As DataTable = New DataTable()

#End Region

#End Region

#Region " Methods "

#Region "Initialize"

    Private Sub Init()

        Me.cbx_ItemParentNo.Focus()

    End Sub

    ''' <summary>
    ''' 設置按鈕狀態
    ''' </summary>
    ''' <param name="type">操作類型</param>
    ''' <param name="IsFind">是否是流覽</param>
    ''' <remarks></remarks>
    Private Sub SetButton(ByVal type As ButtonType, ByVal IsFind As Boolean)
        Try

            Select Case type
                Case ButtonType.Add
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Browse
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True
                        .ToolStripCheck.Enabled = False
                        .ToolStripComplete.Enabled = False
                        .ToolStripUnCheck.Enabled = False
                    End With

                Case ButtonType.Cancel
                    With MainForm
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Edit
                    With MainForm
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

            End Select
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 返回數據集
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ReturnInitDset() As DataSet
        Dim sql As String = ""
        Dim ds As New DataSet()
        Try

            sql = " select * from CS_CusterServiceTypes --where fType=1 "

            ds = Exec(sql, E_ExecType.DataSet)

        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

        Return ds
    End Function

#End Region

#Region " Exec Find "

    Private Function ReturnWhereExp() As String
        Dim sql As String = ""

        If Me.fLoadBool = False Then
            sql &= "  where 1<>1 "
        Else

            sql = "  where 1=1 and not exists (select * from CS_CusterServiceType where ItemParentNo=fCode ) "

            If Me.cbx_ItemParentNo.Text.Trim <> "" AndAlso Me.cbx_ItemParentNo.SelectedValue & "" <> "" Then
                sql &= "  and ItemParentNo='" & Me.cbx_ItemParentNo.SelectedValue & "' "
            End If

            If Me.cbx_ItemNo1.Text.Trim <> "" AndAlso Me.cbx_ItemNo1.SelectedValue & "" <> "" Then
                sql &= "  and ItemNo = '" & Me.cbx_ItemNo1.SelectedValue & "" & "'"
            End If

            If Me.txt_Remark.Text.Trim <> "" Then
                sql &= " and Remark like '%" & Me.txt_Remark.Text.Trim & "%' "
            End If

        End If

        Return sql
    End Function

    Private Sub ExecFind(ByVal whereExp As String)
        Dim sql As String = ""
        Dim Dtab As New DataTable()

        Try
            sql &= " select BoNum,ItemParentNo,ItemNo,ItemNo2,b.fName as ItemParentName,c.fName as ItemName,d.FName as ItemName2, Isnull(b.fName,'')+ " + vbNewLine
            sql += "Case when Isnull(c.fName,'')='' then '' else '\'+c.fName end + case when isnull(d.fName,'')='' then '' else '\'+d.fName end as ItemFullName,Remark,flag from dbo.CS_ComplaintRemark a" & vbCrLf
            sql &= " left join dbo.CS_CusterServiceTypes b on Isnull(a.ItemParentNo,'')=b.fCode" & vbCrLf
            sql &= " left join dbo.CS_CusterServiceTypes c on Isnull(a.ItemNo,'')=c.fCode" & vbCrLf
            sql &= " left join dbo.CS_CusterServiceTypes d on Isnull(a.ItemNo2,'')=d.fCode" & vbCrLf
            sql &= " " & whereExp

            Dtab = Exec(sql, E_ExecType.DataTable)

            Me.DGView_Master.DataSource = Dtab
            BindDataGridViewCaption(Me.DGView_Master, Me.ReturnCaption(1))
            FullFormatDataGridView(Me.DGView_Master, False, False)
            Me.DGView_Master.SelectionMode = DataGridViewSelectionMode.CellSelect
            SetWidth()
            If Me.fLoadBool = True Then
                SetButton(ButtonType.Browse, Me.DGView_Master.Rows.Count > 0)
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Function ReturnCaption(ByVal index) As Hashtable
        Dim hash As New Hashtable

        Select Case index
            Case 1

                hash.Add("BoNum", "單號")
                hash.Add("ItemFullName", "來電類別")
                hash.Add("Remark", "內容")
            Case 2
                hash.Add("ItemNo", "序號")
                hash.Add("Solution", "解決方案")
                hash.Add("Remark", "備注")
            Case 3

        End Select



        Return hash
    End Function

#End Region

#Region "FillCombox"

    Private Sub FillItemParentcbx(ByRef dt As DataTable)
        Dim dv As DataView = dt.DefaultView

        dv.RowFilter = "fLevel=0"

        Me.cbx_ItemParentNo.DataSource = dv
        Me.cbx_ItemParentNo.DisplayMember = "fName"
        Me.cbx_ItemParentNo.ValueMember = "fCode"

        Me.cbx_ItemParentNo.SelectedIndex = -1
        Me.cbx_ItemParentNo.Text = ""

    End Sub

    Private Sub FillItemParentcbx_1(ByRef dt As DataTable)
        Dim dv As DataView = dt.DefaultView

        dv.RowFilter = "fLevel=0"

        Me.cbxItemParentNo.DataSource = dv
        Me.cbxItemParentNo.DisplayMember = "fName"
        Me.cbxItemParentNo.ValueMember = "fCode"

        Me.cbxItemParentNo.SelectedIndex = -1
        Me.cbxItemParentNo.Text = ""

    End Sub

#End Region

#Region "FillDetail"


    Private Sub FillDetailGrid_1(ByVal BoNum As String)
        Dim Dtab As DataTable = New DataTable
        Try
            Dim sql As String = " select * from dbo.CS_SolutionRemark where BoNum= '" & BoNum & "'  "

            Dtab = Exec(sql, E_ExecType.DataTable)
            Me.DGV_Detail.DataSource = Dtab
            BindDataGridViewCaption(Me.DGV_Detail, Me.ReturnCaption(2))
            FullFormatDataGridView(Me.DGV_Detail, False, False)
            Me.DGV_Detail.SelectionMode = DataGridViewSelectionMode.CellSelect
            Me.DGV_Detail.Columns("ItemNo").Width = 80
            Me.DGV_Detail.Columns("Solution").Width = 600
            Me.DGV_Detail.Columns("Remark").Width = 200
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub FilDetail(ByVal BoNum As String)
        Dim Dtab As DataTable = DirectCast(Me.DGView_Master.DataSource, DataTable)

        Dim DRow() As DataRow = Dtab.Select("BoNum='" & BoNum & "'")
        If DRow.Length > 0 Then
            Me.txtBoNum.Text = DRow(0).Item("BoNum") & ""
            Me.cbxItemParentNo.Text = DRow(0).Item("ItemParentName") & ""
            Me.cbxItemNo1.Text = DRow(0).Item("ItemName") & ""
            Me.cbxItemNo2.Text = DRow(0).Item("ItemName2").ToString
            Me.txtRemark.Text = DRow(0).Item("Remark") & ""
        Else
            Me.txtBoNum.Text = ""
            Me.cbxItemParentNo.Text = ""
            Me.cbxItemNo1.Text = ""
            Me.cbxItemNo2.Text = ""
            Me.txtRemark.Text = ""
        End If
        FillDetailGrid_2(BoNum)
    End Sub

    Private Sub FillDetailGrid_2(ByVal BoNum As String)
        Dim Dtab As DataTable = New DataTable
        Try
            Dim sql As String = " select * from dbo.CS_SolutionRemark where BoNum= '" & BoNum & "'  "

            Dtab = Exec(sql, E_ExecType.DataTable)
            Me.DataGridView1.DataSource = Dtab
            BindDataGridViewCaption(Me.DataGridView1, Me.ReturnCaption(2))
            FullFormatDataGridView(Me.DataGridView1, False, False)
            Me.DataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect
            Me.DataGridView1.Columns("ItemNo").Width = 80
            Me.DataGridView1.Columns("Solution").Width = 600
            Me.DataGridView1.Columns("Remark").Width = 200
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region

#Region "AddHandler Events"

    ''' <summary>
    ''' KeyDown Event
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub TextBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
        Try
            Select Case e.KeyCode
                Case Keys.Enter
                    If e.Shift = True Then
                        If TypeOf sender Is ComboBox Then
                            If CType(sender, ComboBox).DroppedDown = True Then
                                CType(sender, ComboBox).DroppedDown = False
                            End If
                        End If
                        Me.ProcessTabKey(False)
                        SendKeys.Send("{F4}")
                    Else
                        If sender.Name.ToString.IndexOf("Remark") <= 0 Then
                            If TypeOf sender Is ComboBox Then
                                If CType(sender, ComboBox).DroppedDown = True Then
                                    CType(sender, ComboBox).DroppedDown = False
                                End If
                            End If
                            Me.ProcessTabKey(True)
                            SendKeys.Send("{F4}")
                        End If
                    End If
            End Select
        Catch ex As Exception
            MsgBox("TextBox_KeyDown:" & ex.Message & ex.StackTrace, MsgBoxStyle.Critical And MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 添加自定義事件
    ''' </summary>
    ''' <param name="Pans"></param>
    ''' <remarks></remarks>
    Private Sub AddHandlers(ByVal Pans() As Control)
        Try
            For Each Pan As Control In Pans
                For Each Ctrl As Control In Pan.Controls
                    If TypeOf Ctrl Is TextBox _
                       OrElse TypeOf Ctrl Is ComboBox _
                       OrElse TypeOf Ctrl Is DateTimePicker Then
                        AddHandler Ctrl.KeyDown, AddressOf TextBox_KeyDown
                    End If
                Next
            Next
        Catch ex As Exception
            MsgBox("AddHandlers:" & ex.Message & ex.StackTrace, MsgBoxStyle.Critical And MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

#End Region

#Region "DataGridView"

    Private Sub SetDataGridEnabled(ByVal bEnabled As Boolean)
        Dim bReadOnly As Boolean = Not bEnabled

        Me.DataGridView1.AllowUserToAddRows = bEnabled
        Me.DataGridView1.AllowUserToDeleteRows = bEnabled
        Me.DataGridView1.AllowUserToResizeRows = bEnabled

        Me.DataGridView1.ReadOnly = bReadOnly
        If bEnabled Then
            Me.DataGridView1.Columns("ItemNo").ReadOnly = True
        End If

    End Sub


#End Region

#Region "SaveData"

    Private Function Validated1() As Boolean
        Me.DGView_Master.EndEdit()

        Me.DGView_Master.Update()
        Me.DGView_Master.Refresh()
        If Me.txtRemark.Text.Trim = "" Then
            MessageBox.Show("內容不能為空。", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Me.txtRemark.Focus()
            Return False
        End If

        If Me.cbxItemParentNo.Text.Trim = "" OrElse Me.cbxItemParentNo.SelectedValue & "" = "" Then
            MessageBox.Show("請選擇來電類別。", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Me.cbxItemParentNo.Focus()
            Return False
        End If
        Return True
    End Function

    Private Function SaveData() As Boolean
        Dim bSuccess As Boolean = False
        Dim BoNum As String = Me.txtBoNum.Text.Trim
        Dim ItemParentNo As String = Me.cbxItemParentNo.SelectedValue & ""
        Dim ItemNo As String = String.Empty
        Dim ItemNo2 As String = String.Empty
        Dim ItemNoText As String = String.Empty
        Dim ItemNo2Text As String = String.Empty
        If Not Me.cbxItemNo1.SelectedValue Is Nothing Then
            ItemNo = Me.cbxItemNo1.SelectedValue.ToString
            ItemNoText = Me.cbxItemNo1.Text.Trim
            If Not Me.cbxItemNo2.SelectedValue Is Nothing Then
                ItemNo2 = Me.cbxItemNo2.SelectedValue.ToString
                ItemNo2Text = Me.cbxItemNo2.Text.Trim
            End If
        End If

        Dim Remark As String = Me.txtRemark.Text.Trim
        Dim dt As DataTable = DirectCast(Me.DataGridView1.DataSource, DataTable)

        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
            BoNum = Exec(" Exec  ReturnBillNum 'CS_ComplaintRemark','BoNum','DG' ", E_ExecType.ExecuteScalar) & ""
            Me.txtBoNum.Text = BoNum
        End If

        Dim detaildt As DataTable = Me.ReturnDataTable(dt, BoNum)

        Dim Text As New System.Text.StringBuilder()
        Dim flag As Integer = 0
        If detaildt.Rows.Count > 0 Then
            flag = 1
        End If
        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
            Text.Append("Insert Into CS_ComplaintRemark(BoNum,ItemParentNo,ItemNo,ItemNo2,Remark,flag)" & System.Environment.NewLine())
            Text.Append("values('" & BoNum & "','" & ItemParentNo & "','" & ItemNo & "','" + ItemNo2 + "','" & Remark & "'," & flag & ")" & System.Environment.NewLine())
        Else
            Text.Append("Update CS_ComplaintRemark set ItemParentNo='" & ItemParentNo & "' ,ItemNo='" & ItemNo & "',ItemNo2='" + ItemNo2 + "',Remark='" & Remark & "',flag=" & flag & " " & System.Environment.NewLine())
            Text.Append("where BoNum='" & BoNum & "' " & System.Environment.NewLine())
            Text.Append("delete CS_SolutionRemark where BoNum='" & BoNum & "'  " & System.Environment.NewLine())
        End If

        Dim sqlcn As SqlConnection = GetConn()
        Dim sqlcmd As New SqlCommand()
        Dim sqlTran As SqlTransaction = sqlcn.BeginTransaction()

        Try

            With sqlcmd
                .CommandText = Text.ToString()
                .Connection = sqlcn
                .CommandType = CommandType.Text
                .Transaction = sqlTran
                .ExecuteNonQuery()
            End With

            Dim sqlBluck As SqlBulkCopy = New SqlBulkCopy(sqlcn, SqlBulkCopyOptions.Default, sqlTran)
            sqlBluck.BatchSize = 100
            sqlBluck.DestinationTableName = "CS_SolutionRemark"
            sqlBluck.WriteToServer(detaildt)
            sqlTran.Commit()
            bSuccess = True
        Catch ex As Exception
            sqlTran.Rollback()
            MessageBox.Show(ex.Message)
            bSuccess = False
        Finally
            sqlcn.Close()
            sqlcn.Dispose()
            sqlcmd.Dispose()
            sqlTran.Dispose()
        End Try
        Dim ItemParentText As String = String.Empty
        ItemParentText = Me.cbxItemParentNo.Text.Trim()
        Dim ItemFullName As String = IIf(isNull(ItemNo2Text), IIf(isNull(ItemNoText), ItemParentText, ItemParentText + "\" + ItemNoText).ToString, ItemParentText + "\" + ItemNoText + "\" + ItemNo2Text).ToString
        UpdateData(BoNum, ItemParentNo, ItemParentText, ItemNo, ItemNo2, IIf(isNull(ItemNo2Text), ItemNoText, ItemNo2Text).ToString, ItemFullName, Remark, flag)

        If bSuccess = True Then
            MessageBox.Show(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.Resources.AddSuccess, My.resources.EditSuccess), Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
        Else
            MessageBox.Show(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.Resources.ErrAdd, My.resources.ErrEdit), Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            Return False
        End If

        Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
        cs.SetControlsEnabled(False)
        ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
        Me.SetButton(ButtonType.Browse, True)
        SetDataGridEnabled(False)
        Return bSuccess
    End Function

    Private Function ReturnDataTable(ByRef dt As DataTable, ByVal BoNum As String)
        Dim resultdt As New DataTable
        With resultdt.Columns
            .Add("BoNum", GetType(String))
            .Add("ItemNo", GetType(Integer))
            .Add("Solution", GetType(String))
            .Add("Remark", GetType(String))
        End With

        ''清除空行
        Dim rows() = dt.Select("Isnull(Solution,'')=''")
        For Each row As DataRow In rows
            row.Delete()
        Next
        dt.AcceptChanges()
        Dim I As Integer = 1

        For Each row As DataRow In dt.Rows
            Dim NRow As DataRow = resultdt.NewRow()
            NRow("BoNum") = BoNum
            NRow("ItemNo") = I
            NRow("Solution") = row("Solution")
            NRow("Remark") = row("Remark")
            I += 1
            resultdt.Rows.Add(NRow)
        Next
        resultdt.AcceptChanges()

        Return resultdt
    End Function

    Private Function UpdateData(ByVal BoNum As String, ByVal ItemParentNo As String, ByVal ItemParentName As String, ByVal ItemNo As String, ByVal ItemNo2 As String, ByVal ItemName As String, ByVal ItemFullName As String, ByVal Remark As String, ByVal flag As Integer)
        Dim Dtab As DataTable = DirectCast(Me.DGView_Master.DataSource, DataTable)

        If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
            Dim Row As DataRow = Dtab.NewRow()

            Row("BoNum") = BoNum
            Row("ItemParentNo") = ItemParentNo
            Row("ItemParentName") = ItemParentName
            Row("ItemNo") = ItemNo
            Row("ItemNo2") = ItemNo2
            Row("ItemName") = ItemName
            'Row("ItemFullName") = ItemParentName & IIf(ItemName <> "", "\" & ItemName, "")
            Row("ItemFullName") = ItemFullName
            Row("Remark") = Remark
            Row("flag") = flag
            Dtab.Rows.Add(Row)

        Else
            Dim DRow() As DataRow = Dtab.Select("BoNum='" & BoNum & "'")

            If DRow.Length > 0 Then

                Dim Row As DataRow = DRow(0)
                Row("ItemParentNo") = ItemParentNo
                Row("ItemParentName") = ItemParentName
                Row("ItemNo") = ItemNo
                Row("ItemNo2") = ItemNo2
                Row("ItemName") = ItemName
                Row("ItemFullName") = ItemFullName
                'Row("ItemFullName") = ItemParentName & IIf(ItemName <> "", "\" & ItemName, "")
                Row("Remark") = Remark
                Row("flag") = flag
            End If
        End If
        Dtab.AcceptChanges()
        Me.DGView_Master.Update()
        Me.DGView_Master.Refresh()
        Return True
    End Function

#End Region

#End Region

#Region " Events "

#Region "Form Events"

    Private Sub Frm_CSRemark_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.fLoadBool = False
        Me.Init()
        Me.AddHandlers(New Control() {Panel1, Me.GroupBox2})
        Dim imeMode As clsIme = New clsIme()
        imeMode.AddHandler_ImeMode(New Control() {Panel1, GroupBox2})
        Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
        cs.SetControlsEnabled(False)
        Dim ds As DataSet = ReturnInitDset()
        Me.dbType = ds.Tables(0)
        FillItemParentcbx(Me.dbType.Copy())
        FillItemParentcbx_1(Me.dbType.Copy())

        Dim whereExp As String = Me.ReturnWhereExp()
        Me.ExecFind(whereExp)
        Me.fLoadBool = True
    End Sub

    Private Sub SetWidth()
        Me.DGView_Master.Columns("BoNum").Width = 100
        Me.DGView_Master.Columns("ItemFullName").Width = 200
        Me.DGView_Master.Columns("Remark").Width = 600
    End Sub

#End Region

#Region "Combox Events"

    Private Sub cbx_ItemParentNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx_ItemParentNo.TextChanged
        If Me.fLoadBool = False Then Exit Sub
        Try
            Dim Key As String = Me.cbx_ItemParentNo.SelectedValue & ""
            If Me.cbx_ItemParentNo.Text.Trim = "" Then
                Key = ""
            End If
            Dim RowFilter As String = "fParentCode='" & Key & "'"
            Dim dv As DataView = New DataView(Me.dbType)
            dv.RowFilter = RowFilter
            Me.cbx_ItemNo1.DataSource = dv
            Me.cbx_ItemNo1.DisplayMember = "fName"
            Me.cbx_ItemNo1.ValueMember = "fCode"
            Me.cbx_ItemNo1.SelectedIndex = -1
            Me.cbx_ItemNo1.Text = ""

        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub cbx_ItemNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx_ItemNo1.TextChanged, cbxItemNo1.TextChanged
        If Me.fLoadBool = False Then Exit Sub
        Try
            Dim cbx As ComboBox = CType(sender, ComboBox)
            Dim Key As String = String.Empty
            If Not cbx.SelectedValue Is Nothing Then
                Key = cbx.SelectedValue.ToString
            End If

            Dim RowFilter As String = "fParentCode='" & Key & "'"

            Dim dv As DataView = New DataView(Me.dbType)
            dv.RowFilter = RowFilter

            If cbx.Equals(Me.cbx_ItemNo1) Then
                Me.cbx_ItemNo2.DataSource = dv
                Me.cbx_ItemNo2.DisplayMember = "fName"
                Me.cbx_ItemNo2.ValueMember = "fCode"
                Me.cbx_ItemNo2.SelectedIndex = -1
                Me.cbx_ItemNo2.Text = ""
            Else
                Me.cbxItemNo2.DataSource = dv
                Me.cbxItemNo2.DisplayMember = "fName"
                Me.cbxItemNo2.ValueMember = "fCode"
                Me.cbxItemNo2.SelectedIndex = -1
                Me.cbxItemNo2.Text = ""
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub cbxItemParentNo_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cbxItemParentNo.TextChanged
        If Me.fLoadBool = False Then Exit Sub

        Try

            Dim Key As String = Me.cbxItemParentNo.SelectedValue & ""

            If Me.cbxItemParentNo.Text.Trim = "" Then
                Key = ""
            End If

            Dim RowFilter As String = "fParentCode='" & Key & "'"

            Dim dv As DataView = Me.dbType.Copy().DefaultView
            dv.RowFilter = RowFilter

            Me.cbxItemNo1.DataSource = dv
            Me.cbxItemNo1.DisplayMember = "fName"
            Me.cbxItemNo1.ValueMember = "fCode"

            Me.cbxItemNo1.SelectedIndex = -1
            Me.cbxItemNo1.Text = ""

        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region

#Region "Button Events"

    Private Sub btnFind_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnFind.Click
        Dim whereExp As String = Me.ReturnWhereExp()
        ExecFind(whereExp)
    End Sub

#End Region

#Region "DataGridView"

    Private Sub DGView_Master_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGView_Master.CellDoubleClick
        Me.TabControl1.SelectedIndex = 1
    End Sub

    Private Sub DGView_Master_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DGView_Master.CurrentCellChanged
        If Me.fLoadBool = False Then Exit Sub

        If ActionHelp <> IDServer.IDServer.ActionBtn.ActionHelp Then Exit Sub
        Try
            Dim BoNum As String = ""
            Dim flag As Integer = 0

            If Not Me.DGView_Master.CurrentRow Is Nothing Then
                BoNum = Me.DGView_Master.CurrentRow.Cells("BoNum").Value & ""
                flag = Val(Me.DGView_Master.CurrentRow.Cells("flag").Value & "")
            End If
            If flag = 1 Then
                FillDetailGrid_1(BoNum)
            Else
                If TypeOf Me.DGV_Detail.DataSource Is DataTable Then
                    DirectCast(Me.DGV_Detail.DataSource, DataTable).Clear()
                    DirectCast(Me.DGV_Detail.DataSource, DataTable).AcceptChanges()
                End If
            End If
            Me.DGV_Detail.Update()
            Me.DGV_Detail.Refresh()
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DGView_Master_RowPostPaint(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DGView_Master.RowPostPaint, DGV_Detail.RowPostPaint
        Try
            Dim Grid As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(Grid.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
            SBrush = Nothing
        Catch ex As Exception
            MessageBox.Show("DGV_Master_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

#End Region

#Region "TabControl"

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        If Me.TabControl1.SelectedIndex = 1 AndAlso Me.ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp Then
            Dim BoNum As String = ""
            If Not Me.DGView_Master.CurrentRow Is Nothing Then
                BoNum = Me.DGView_Master.CurrentRow.Cells("BoNum").Value & ""
            End If
            FilDetail(BoNum)
        End If
    End Sub

#End Region

#End Region

#Region " Interface "

    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        Try
            If AddEnable = False Then
                MessageBox.Show("對不起，您沒有新增權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.TabControl1.SelectedIndex = 0 Then
                Me.TabControl1.SelectedIndex = 1
            End If
            Me.FilDetail("")
            Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
            cs.SetControlsEnabled(True)
            Me.txtBoNum.ReadOnly = True
            Me.cbxItemParentNo.Focus()
            ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
            Me.SetButton(ButtonType.Add, False)
            SetDataGridEnabled(True)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK)
        End Try
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Try
            Dim BoNum As String = ""
            If Not Me.DGView_Master.CurrentRow Is Nothing Then
                BoNum = Me.DGView_Master.CurrentRow.Cells("BoNum").Value & ""
            End If
            Me.FilDetail(BoNum)
            Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
            cs.SetControlsEnabled(False)
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp
            Me.SetButton(ButtonType.Browse, Me.DGView_Master.Rows.Count > 0)
            SetDataGridEnabled(False)
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        Try
            If DeleteEnable = False Then
                MessageBox.Show("對不起，您沒有刪除權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.DGView_Master.CurrentRow Is Nothing Then Return False

            If MessageBox.Show(My.Resources.ConfirmDelete, Msg.CompanyName, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) <> DialogResult.OK Then Return False

            Dim BoNum As String = Me.DGView_Master.CurrentRow.Cells("BoNum").Value & ""
            Dim RowIndex As Integer = Me.DGView_Master.CurrentRow.Index
            Dim sql As String = " "

            sql &= " Delete dbo.CS_SolutionRemark where BoNum='" & BoNum & "'" & vbCrLf
            sql &= " Delete dbo.CS_ComplaintRemark where BoNum='" & BoNum & "'" & vbCrLf
            Dim I As Integer = 0
            I = Val(Exec(sql, E_ExecType.ExecuteNonQuery, True) & "")

            Dim Dtab As DataTable = DirectCast(Me.DGView_Master.DataSource, DataTable)
            Dim DRow() As DataRow = Dtab.Select("BoNum='" & BoNum & "'")
            If DRow.Length > 0 Then
                DRow(0).Delete()
            End If
            Dtab.AcceptChanges()
            Me.DGView_Master.Update()
            Me.DGView_Master.Refresh()
            RowIndex -= 1
            If Me.DGView_Master.Rows.Count > 0 Then
                If RowIndex <= Me.DGView_Master.Rows.Count - 1 Then
                    Me.DGView_Master.CurrentCell = Me.DGView_Master.Rows(RowIndex).Cells("BoNum")
                Else
                    Me.DGView_Master.CurrentCell = Me.DGView_Master.Rows(0).Cells("BoNum")
                End If
            End If
            If Me.TabControl1.SelectedIndex = 1 Then
                If Not Me.DGView_Master.CurrentRow Is Nothing Then
                    BoNum = Me.DGView_Master.CurrentRow.Cells("BoNum").Value & ""
                Else
                    BoNum = ""
                End If
                Me.FilDetail(BoNum)
            End If
            MessageBox.Show(My.resources.DeleteSuccess, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.SetButton(ButtonType.Browse, Me.DGView_Master.Rows.Count > 0)
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return True
    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        Try
            If EditEnable = False Then
                MessageBox.Show("對不起，您沒有修改權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            If Me.TabControl1.SelectedIndex = 0 Then
                Me.TabControl1.SelectedIndex = 1
            End If
            Dim cs As setControlEnabled = New setControlEnabled(New Control() {Me.GroupBox2})
            cs.SetControlsEnabled(True)
            Me.txtBoNum.ReadOnly = True
            Me.txtRemark.Focus()
            Me.txtRemark.SelectionStart = Me.txtRemark.Text.Length
            ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
            SetDataGridEnabled(True)
            Me.SetButton(ButtonType.Edit, Me.DGView_Master.Rows.Count > 0)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK)
        End Try
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        If Me.Validated1() = False Then Return True
        Dim bSuccess As Boolean = SaveData()
        Return bSuccess
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region


End Class
